function FakeItBaby (onloadFn, filter = '', isRegexp = false) {
  if (typeof onloadFn !== 'function') throw new Error('第一个参数必须是函数。接受”接口结果“和”url“作为两个参数。')

  var fn = (xhrInstance) => {
    const args = [
      {
        responseText: xhrInstance.response,
        url: xhrInstance.responseURL,
        headers: xhrInstance.getAllResponseHeaders(),
        xhrInstance: xhrInstance,
      },
    ]

    if (!filter) {
      // 监听所有
      onloadFn(...args)
    } else if (isRegexp) {
      if (isRegexp.match(xhrInstance.responseURL)) onloadFn(...args)
    } else {
      if (xhrInstance.responseURL === filter) onloadFn(...args)
    }
  }

  var _GET_XHR = window.XMLHttpRequest
  if (_GET_XHR.isFake) {
    console.error('已经入侵过, 停止处理')
    _GET_XHR = window.XMLHttpRequest.raw
  }

  /**
   * fn 是一个函数，所有的请求的结果，都会被调用一次
   */
  window.XMLHttpRequest = function FakeXHR() {
    var client = new _GET_XHR()
    let reponseListener = function () {}
    client.onload = function () {
      console.log('DEBUG:: onload')
  
      fn(client)
  
      if (reponseListener) reponseListener()
    }
  
    return {
      UNSENT: client.UNSENT,
      OPENED: client.OPENED,
      HEADERS_RECEIVED: client.HEADERS_RECEIVED,
      LOADING: client.LOADING,
      DONE: client.DONE,
  
      upload: () => client.upload.call(arguments, client),
      open() { client.open(...arguments); return this },
      send: () => client.send(...arguments),
      abort: () => client.abort(),
      setRequestHeader () { client.setRequestHeader(...arguments) },
      getResponseHeader: (v) => client.getResponseHeader(v),
      getAllResponseHeaders: () => client.getAllResponseHeaders(),
      overrideMimeType: () => client.overrideMimeType.call(arguments, client),
      get response () { return client.response },
      get responseURL() { return client.responseURL },
      get status() { return client.status },
      get statusText() { return client.statusText },
      get responseType() { return client.responseType },
      get responseText() { return client.responseText },
      get responseXML() { return client.responseXML },
      get withCredentials() { return client.withCredentials },
      set withCredentials(bool) { client.withCredentials = bool },
      get timeout() { return client.timeout },
      set timeout(num) { client.timeout = num },
      get readyState() { return client.readyState },
      set readyState(num) { client.readyState = num },
      get onreadystatechange() { return client.onreadystatechange },
      set onreadystatechange(fn) { client.onreadystatechange = fn },
      set onload(fn) {
        console.log('DEBUG:: set onload')
        reponseListener = fn
      },
      get onload() {
        return reponseListener
      },
    }
  }
  window.XMLHttpRequest.isFake = true
  window.XMLHttpRequest.raw = _GET_XHR
}
